package com.my.ribbon.predicate;

import com.alibaba.cloud.nacos.ribbon.NacosServer;
import com.my.core.util.spring.BeanUtil;
import com.my.ribbon.support.CustomRibbonRuleProperties;
import io.micrometer.core.instrument.util.StringUtils;

import java.util.Map;

/**
 * @author timo 2021/3/23
 */
public class MetadataAwarePredicate extends DiscoveryEnabledPredicate {
    public static final MetadataAwarePredicate INSTANCE = new MetadataAwarePredicate();

    public MetadataAwarePredicate() {
    }

    @Override
    protected boolean apply(NacosServer server) {
        Map<String, String> metadata = server.getMetadata();
        CustomRibbonRuleProperties properties = (CustomRibbonRuleProperties) BeanUtil.getBean(CustomRibbonRuleProperties.class);
        String localTag = properties.getTag();
        if (StringUtils.isBlank(localTag)) {
            return true;
        } else {
            String metaDataTag = (String)metadata.get("tag");
            return StringUtils.isBlank(metaDataTag) ? false : metaDataTag.equals(localTag);
        }
    }
}
